//
//  YFUpheadIocnVC.m
//  SquashSpark
//
//  Created by Coollang on 2017/11/15.
//  Copyright © 2017年 Coollang-YF. All rights reserved.
//

#import "YFUpheadIocnVC.h"
#import <SDWebImage/UIImageView+WebCache.h>
#import <SquashSpark-Swift.h>
#import "PhotoActionSheetViewController.h"
#import "SystemSelectPhotoManager.h"
#import "YFLoginApi.h"
#import "UIImage+HXExtension.h"
#import "YFPersonDataVC.h"
#import "YFLoginHelper.h"

@interface YFUpheadIocnVC ()

@property (weak, nonatomic) IBOutlet UIButton *nextStepButton;
@property (weak, nonatomic) IBOutlet UIImageView *headIconImageView;
@property (weak, nonatomic) IBOutlet UILabel *bigTitlelabel;
@property (weak, nonatomic) IBOutlet UILabel *smallTiplabel;
@property (nonatomic, strong) SystemSelectPhotoManager *photoManger;

@end

@implementation YFUpheadIocnVC

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    [_nextStepButton clipsCornerRadius:3];
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(uploadHeadIconImage:)];
    [_headIconImageView addGestureRecognizer:tap];
    [self.headIconImageView sd_setImageWithURL:[NSURL URLWithString:_cacheUserInfoItem.icon] placeholderImage:kSquashLogoPlaceholdImage options:SDWebImageLowPriority|SDWebImageRetryFailed];
    [self addAnimation];
    
    if (self.cacheUserInfoItem) {
        [self.cacheUserInfoItem addObserver:self forKeyPath:@"icon" options:NSKeyValueObservingOptionNew context:nil];
    }
}

- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary<NSKeyValueChangeKey,id> *)change context:(void *)context {
    
    if ([keyPath isEqualToString:@"icon"]) {
         [self.headIconImageView sd_setImageWithURL:[NSURL URLWithString:_cacheUserInfoItem.icon] placeholderImage:kSquashLogoPlaceholdImage options:SDWebImageLowPriority|SDWebImageRetryFailed];
    }
}

- (void)dealloc {
    if (self.cacheUserInfoItem) {
        [self.cacheUserInfoItem removeObserver:self forKeyPath:@"icon"];
    }
}

- (void)viewDidLayoutSubviews {
    [super viewDidLayoutSubviews];
    //[self.headIconImageView clipsCornerRadius:self.headIconImageView.bounds.size.width * 0.5];
    [self.headIconImageView clipsCornerRadius:8];
}

//- (void)viewWillAppear:(BOOL)animated {
//    [super viewWillAppear:animated];
//    [self.navigationController setNavigationBarHidden:YES animated:YES];
//}
//- (void)viewWillDisappear:(BOOL)animated {
//    [self.navigationController setNavigationBarHidden:NO animated:YES];
//}

- (void)addAnimation {
    [[AnimationHelper helper] animationOpectiyAndScale:self.nextStepButton];
    [[AnimationHelper helper] animationOpectiyAndScale:self.headIconImageView];
    [[AnimationHelper helper] animationOpectiyAndScale:self.bigTitlelabel];
    [[AnimationHelper helper] animationOpectiyAndScale:self.smallTiplabel];
}

- (void)viewDidAppear:(BOOL)animated {
    [super viewDidAppear:animated];
    [self.headIconImageView sd_setImageWithURL:[NSURL URLWithString:_cacheUserInfoItem.icon] placeholderImage:kSquashLogoPlaceholdImage options:SDWebImageLowPriority|SDWebImageRetryFailed];
}

- (void)uploadHeadIconImage:(UITapGestureRecognizer *)tap {
    AppDelegate *appdelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
    
    [PhotoActionSheetViewController showInParentViewController:appdelegate.window.rootViewController alertType:UIAlertControllerStyleAlert animationType:DetailViewControllerPresentAnimationTypeUp actionBlock:^(kTapActionButtonType btnType) {
        
        if (btnType != kTapCacnelButton) {
            SystemSelectPhotoManager *photoManger = [SystemSelectPhotoManager selectPhotoManagerWithEditCaseType:kSystemDeflaut];
            self.photoManger = photoManger;
            [photoManger takePhotoBySource:btnType canEdit:YES parentViewController:self selectCallBlock:^(UIImage *selectImage, NSError *error) {
                // 获取照片
                [MBProgressHUD showHUDAddedTo:self.view animated:YES];
                [YFLoginHelper uploadUserHeadIconImage:selectImage completed:^(NSString *url, NSError *error) {
                    [MBProgressHUD hideHUDForView:self.view animated:YES];
                    if (error == nil ) {
                       [MBProgressHUD showSuccessHudInView:self.view message:@"Upload image succeed" afterDelay:1.0];
                        _cacheUserInfoItem.icon = url;
                       [self.headIconImageView sd_setImageWithURL:[NSURL URLWithString:url] placeholderImage:kSquashLogoPlaceholdImage options:SDWebImageLowPriority|SDWebImageRetryFailed];
                    }else {
                       [MBProgressHUD showTextHUDWithMessage:@"Upload image fail" inView:self.view];
                    }
                }];
            }];
        }
    }].isTapCoverViewDismiss = YES;
}

- (IBAction)nextStepButtonClick:(id)sender {
    if (self.nextButtonClick) {
        self.nextButtonClick();
    }
//     YFPersonDataVC *personVc = [[YFPersonDataVC alloc] init];
//    [self.navigationController pushViewController:personVc animated:YES];
}


@end
